#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# ----------------------------------------------------------
# -- 
# --
# ****************************
# Author: lmay.Zhou
# Blog: www.lmaye.com
# Date: 2021/7/6 17:10
# Email lmay@lmaye.com
# ----------------------------------------------------------
from core import LOG
from datetime import datetime

import pandas

from core.utils import excel_utils


def read1(excel_file, out_file):
    start = datetime.now()
    LOG.info("任务开始: {}".format(start))
    # 读取excel文件
    names = ["取平均成本条件", "单位直接材料", "单位直接人工", "单位制造费用", "单位其他费用"]
    data_ = pandas.read_excel(excel_file, sheet_name=1, usecols=names)
    LOG.info("取数表-数据: \n{}".format(data_))
    records = pandas.read_excel(excel_file, engine="openpyxl", sheet_name=0)
    end = datetime.now()
    LOG.info("读取数据结束: {}".format(end))
    LOG.info("读取数据耗时: {}".format((end - start).total_seconds()))
    # 合并数据
    rec_merge = pandas.merge(left=records, right=data_, left_on="取平均成本条件", right_on="取平均成本条件", how="left")
    new_columns = rec_merge.columns.to_list()
    # 按逆序insert
    for name in names[:0:-1]:
        new_columns.remove(name)
        new_columns.insert(new_columns.index("记账本位币币别") + 1, name)
    rec_merge = rec_merge.reindex(columns=new_columns)
    rec_merge.to_excel(out_file, index=False)
    end2 = datetime.now()
    LOG.info("写入数据结束: {}".format(end2))
    LOG.info("写入数据耗时: {}".format((end2 - end).total_seconds()))


def read2(excel_file, out_file):
    # 读取excel文件
    start = datetime.now()
    LOG.info("{}".format(start))
    records = excel_utils.read_excel(excel_file, 0)
    end = datetime.now()
    LOG.info("{}".format(end))
    LOG.info("{}".format((end - start).total_seconds()))
